package m.jclecai.com.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import m.jclecai.com.R;

import java.util.List;

import m.jclecai.com.bean.AccountParticularsBean;

/**
 * Created by hp on 2017/4/26.
 */

public class AccountParticularsAdapter extends BaseAdapter{
    private Context context;
    private List<AccountParticularsBean> list;
    public AccountParticularsAdapter(){

    }
    public AccountParticularsAdapter(Context context,  List<AccountParticularsBean> list){
        this.context=context;
        this.list=list;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.account_particulars_item,parent,false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        if(list.size()==1){
            childViewHolder.iv_divider.setVisibility(View.GONE);
        }else {
            childViewHolder.iv_divider.setVisibility(View.VISIBLE);
        }
        AccountParticularsBean bean = list.get(position);
        AccountParticularsBean nBean = null;
        String oTime = "";
        if(position - 1 > -1){
            nBean = list.get(position-1);
            oTime = nBean.getAddtime().substring(5, 10);
        }
        String addtime = bean.getAddtime();
        String subYear = addtime.substring(0, 4);
        String subday = addtime.substring(5, 10);
        String subtime = addtime.substring(11, 16);
        childViewHolder.tv_day.setVisibility(View.VISIBLE);
        childViewHolder.v_circle.setVisibility(View.VISIBLE);
        if(nBean != null && oTime.equals(subday)){
            childViewHolder.tv_day.setVisibility(View.GONE);
            childViewHolder.v_circle.setVisibility(View.GONE);
        }
        childViewHolder.tv_day.setText(Html.fromHtml(String.format(context.getString(R.string.sc_balance_record),subYear,subday)));
        childViewHolder.tv_balance.setText("余额:"+bean.getBalance());
        childViewHolder.tv_name.setText(bean.getOrderName());
        childViewHolder.tv_time.setText(subtime);
        //撤单返款
        if("0".equals(bean.getItype())){
            childViewHolder.tv_money.setText("+" +bean.getBack_money());
            childViewHolder.tv_money.setTextColor(ContextCompat.getColor(context,R.color.font_red));
        }else {
            childViewHolder.tv_money.setText("-" + bean.getGo_money());
            childViewHolder.tv_money.setTextColor(ContextCompat.getColor(context,R.color.font_green));
        }
        return convertView;
    }
    static class ChildViewHolder {

        private final TextView tv_day;
        private final TextView tv_name;
        private final TextView tv_money;
        private final TextView tv_time;
        private final TextView tv_balance;
        private final View iv_divider,v_circle;

        public ChildViewHolder(View view) {
            tv_day = (TextView) view.findViewById(R.id.tv_day);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_money = (TextView) view.findViewById(R.id.tv_money);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_balance = (TextView) view.findViewById(R.id.tv_balance);
            iv_divider = view.findViewById(R.id.iv_divider);
            v_circle = view.findViewById(R.id.v_circle);
        }
    }
}
